﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Data;

namespace FRL.DebtChased.Controls
{
    public partial class AgentBankAccountsCtrl : UserControl, INotifyPropertyChanged
    {

        #region INotifyPropertyChanged handlers
        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

        #region Constructor
        public AgentBankAccountsCtrl()
        {
            InitializeComponent();
            this.LayoutRoot.DataContext = this;

            this.AgentBankAccounts = new ObservableCollection<AgentBankAccountDummy>();

        }
        #endregion

        #region Public Members
        private ObservableCollection<AgentBankAccountDummy> _agentBankAccounts;
        public ObservableCollection<AgentBankAccountDummy> AgentBankAccounts
        {
            get { return _agentBankAccounts; }
            set
            {
                if (ActiveRecordEdited) SaveRecordChanges();

                this._agentBankAccounts = value;
                RaisePropertyChanged("BankAccounts");

                if (_agentBankAccounts == null) ActiveRecord = null;
                else if (_agentBankAccounts.Count > 0) ActiveRecord = _agentBankAccounts[0];
                else ActiveRecord = NewRecord;
            }
        }
        #endregion

        #region Tab Management
        private AgentBankAccountDummy _newRecord;
        private AgentBankAccountDummy NewRecord
        {
            get
            {
                if (_newRecord == null)
                    _newRecord = new AgentBankAccountDummy();
                return _newRecord;
            }
            set { _newRecord = value; }
        }

        private AgentBankAccountDummy _activeRecord;
        public AgentBankAccountDummy ActiveRecord
        {
            get { return _activeRecord; }
            set
            {
                if ((object.ReferenceEquals(this._activeRecord, value) != true))
                {
                    if (ActiveRecordEdited) SaveRecordChanges();
                    ActiveRecordEdited = false;

                    this._activeRecord = value;
                    RaisePropertyChanged("ActiveRecord");
                    RaisePropertyChanged("RecordTabs");
                }
            }
        }

        private bool _activeRecordEdited;
        public bool ActiveRecordEdited
        {
            get { return _activeRecordEdited; }
            set
            {
                if (value != _activeRecordEdited)
                {
                    _activeRecordEdited = value;
                    RaisePropertyChanged("ActiveRecordEdited");
                }
            }
        }


        public IEnumerable<TabItem> RecordTabs
        {
            get
            {
                List<TabItem> tabs = new List<TabItem>();
                if (AgentBankAccounts != null)
                {
                    foreach (AgentBankAccountDummy record in AgentBankAccounts)
                    {
                        TabItem tab = new TabItem() { Header = string.Format("{0}", record.AccNumber), Tag = record };
                        tabs.Add(tab);
                    }
                    tabs.Add(new TabItem() { Header = "*New*", Tag = NewRecord });
                }
                return tabs;
            }
        }

        private bool _isRecordEditable = true;
        public bool IsRecordEditable
        {
            get { return _isRecordEditable; }
            set {
                if (value != _isRecordEditable)
                {
                    _isRecordEditable = value;
                    RaisePropertyChanged("IsRecordEditable");
                }
            }

        }

        private void TextBox_ContentChanged(object sender, TextChangedEventArgs e)
        {
            ActiveRecordEdited = true;
        }
        private void SaveRecordChanges()
        {
            ActiveRecordEdited = false;
        }
        private void TextBox_AccNumberChanged(object sender, TextChangedEventArgs e)
        {
            if (AgentBankAccounts != null)
            {
                if (_activeRecord == _newRecord)
                {
                    AgentBankAccounts.Add(_activeRecord);
                    _newRecord = null;
                    ActiveRecordEdited = false;
                }
            }

            RaisePropertyChanged("RecordTabs");
        }


        private void ContactTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            TabControl ctrl = (TabControl)sender;
            if (ctrl.SelectedIndex >= 0)
            {
                TabItem tab = (TabItem)ctrl.Items[ctrl.SelectedIndex];
                ActiveRecord = (AgentBankAccountDummy)tab.Tag;
            }
            else if (ActiveRecord != null && ctrl.Items != null)
            {
                foreach (TabItem tab in ctrl.Items)
                {
                    if (tab.Tag == ActiveRecord)
                    {
                        ctrl.SelectedItem = tab;
                        break;
                    }
                }
            }
        }
        #endregion

        #region General Event Handlers
        private void HeaderBar_ExpandChange(object sender, EventArgs e)
        {
            if (HeaderBar.IsArrowUp)
            {
                LayoutRoot.RowDefinitions[1].Height = new GridLength(0);
                HeaderBar.IsArrowUp = false;
            }
            else
            {
                LayoutRoot.RowDefinitions[1].Height = new GridLength(1,GridUnitType.Auto);
                HeaderBar.IsArrowUp = true;
            }
        }
        #endregion

        private void TestClick(object sender, RoutedEventArgs e)
        {
            IsRecordEditable = !IsRecordEditable;
        }

    }


    public class AgentBankAccountDummy
    {
        public string Bank { get; set; }
        public string Currency { get; set; }
        public string AccName { get; set; }
        public string SortCode { get; set; }
        public string AccNumber { get; set; }
        public string IBAN { get; set; }
        public string SwiftCode { get; set; }
        public string AddressLine1 { get; set; }
        public string AddressLine2 { get; set; }
        public string AddressLine3 { get; set; }
        public string AddressCity { get; set; }
        public string AddressRegion { get; set; }
        public string AddressPostcode { get; set; }
        public string AddressCountry { get; set; }

    }
}
